/*******************************************************************************
 * Copyright 2011 WaTho
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package de.wathoserver.android.dragoid.activity.singlebuilding;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import de.wathoserver.android.dragoid.R;
import de.wathoserver.android.dragoid.api.Building;
import de.wathoserver.android.dragoid.api.BuildingUpgrade;
import de.wathoserver.android.dragoid.api.DragoApi;
import de.wathoserver.android.dragoid.api.DragoStore;
import de.wathoserver.android.dragoid.api.DragoidConstants;

/**
 * Tab mit den Aus- und Umbaumöglichkeiten eines Gebäudes
 * 
 * @author watho
 * 
 */
public class SingleBuildingUpgradeActivity extends ListActivity implements
		DragoidConstants {

	Building building;
	private String[] col_names;
	private int[] view_ids;
	private int pos;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		col_names = new String[] { BuildingUpgrade.COL_NAME,
				BuildingUpgrade.COL_PRODUCES,
				BuildingUpgrade.COL_REQUIREMENT_STOCK,
				BuildingUpgrade.COL_MISSINGSTOCK };
		view_ids = new int[] { R.id.singlebuilding_upgrade_name,
				R.id.singlebuilding_upgrade_produces,
				R.id.singlebuilding_upgrade_requirement_stock,
				R.id.singlebuilding_upgrade_missingstock };

		Intent parentIntent = getIntent();
		pos = parentIntent.getIntExtra(Building.COL_POSITION, -1);
	}

	@Override
	protected void onResume() {
		super.onResume();
		reload();
	}

	public void reload() {
		building = DragoStore.getDragoStore().getBuildings().get(pos);
		setListAdapter(new SimpleAdapter(this, building.getUpgradesAsMap(),
				R.layout.singlebuilding_upgrade_row, col_names, view_ids));
		ListView lv = getListView();
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int positon, long id) {
				final BuildingUpgrade bu = building.getUpgrades().get(positon);
				if (bu.isPrepared()) {
					AlertDialog.Builder builder = new AlertDialog.Builder(
							SingleBuildingUpgradeActivity.this);
					builder.setMessage(
							String.format(
									"Soll der %s zu %s wirklich gestartet werden?\nDas kann nicht rückgängig gemacht werden.",
									bu.getTypeStr(), bu.getName()))
							.setCancelable(true)
							.setPositiveButton("Ja",
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											try {
												DragoApi.getApi()
														.upgradeBuilding(
																building, bu);
												reload();
											} catch (IllegalAccessException e) {
												Log.e(LOG_TAG,
														"Fehler beim Aus/Umbau des Gebäudes",
														e);
											}
										}
									})
							.setNegativeButton("Nein",
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											dialog.cancel();
										}
									});
					AlertDialog alert = builder.create();
					alert.show();
				} else {
					Toast.makeText(SingleBuildingUpgradeActivity.this,
							"Es fehlen Materialien", Toast.LENGTH_LONG).show();
				}
			}
		});
	}
}
